package com.example.studentalarm.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.studentalarm.bean.TimetableBean;
import com.example.studentalarm.constant.DbConstant;
import com.example.studentalarm.utils.LogUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator
 * <p/>
 * 数据库管理层
 */
public class DbManager {
    static private DbManager dbMgr = new DbManager();
    private DbOpenHelper dbHelper;

    public void onInit(Context context) {
        dbHelper = DbOpenHelper.getInstance(context);
    }

    public static synchronized DbManager getInstance() {
        return dbMgr;
    }


    synchronized public void closeDB() {
        if (dbHelper != null) {
            dbHelper.closeDB();
        }
    }


    /**
     * 查询课程表
     **/
    synchronized public List<TimetableBean> queryTimetableList() {
        List<TimetableBean> list = new ArrayList<>();
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            if (db.isOpen()) {
                String sql = "SELECT * FROM " + DbConstant.TIMETABLE.TABLE_NAME;
                Cursor cursor = db.rawQuery(sql, null);
                while (cursor.moveToNext()) {
                    TimetableBean timetableBean = new TimetableBean();
                    timetableBean.setId(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ID)));
                    timetableBean.setName(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_NAME)));
                    timetableBean.setTime(new Date(cursor.getLong(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_TIME))));
                    timetableBean.setAddress(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ADDRESS)));
                    timetableBean.setNote(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_NOTE)));
                    timetableBean.setAlarmState(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ALARM_STATE)));
                    timetableBean.setMinute(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_MINUTE)));
                    timetableBean.setDate(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_DATE)));
                    list.add(timetableBean);
                }
                cursor.close();
                return list;
            }
        }
        return list;
    }

    /**
     * 根据日期查询课程表列表
     **/
    synchronized public List<TimetableBean> queryTimetableListByDate(String date) {
        List<TimetableBean> list = new ArrayList<>();
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            if (db.isOpen()) {
                String sql = "SELECT * FROM " + DbConstant.TIMETABLE.TABLE_NAME + " where " + DbConstant.TIMETABLE.COLUMN_NAME_DATE + " = ?" + " order by time";
                Cursor cursor = db.rawQuery(sql, new String[]{date});
                while (cursor.moveToNext()) {
                    TimetableBean timetableBean = new TimetableBean();
                    timetableBean.setId(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ID)));
                    timetableBean.setName(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_NAME)));
                    timetableBean.setTime(new Date(cursor.getLong(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_TIME))));
                    timetableBean.setAddress(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ADDRESS)));
                    timetableBean.setNote(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_NOTE)));
                    timetableBean.setAlarmState(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ALARM_STATE)));
                    timetableBean.setMinute(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_MINUTE)));
                    timetableBean.setDate(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_DATE)));
                    list.add(timetableBean);
                }
                cursor.close();
                return list;
            }
        }
        return list;
    }


    /**
     * 根据ID查询课程表
     **/
    synchronized public TimetableBean queryTimetableById(Integer id) {
        TimetableBean timetableBean = new TimetableBean();
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            if (db.isOpen()) {
                String sql = "SELECT * FROM " + DbConstant.TIMETABLE.TABLE_NAME + " where id=?" + "order by id desc";
                Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id)});
                while (cursor.moveToNext()) {
                    timetableBean.setId(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ID)));
                    timetableBean.setName(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_NAME)));
                    timetableBean.setTime(new Date(cursor.getLong(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_TIME))));
                    timetableBean.setAddress(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ADDRESS)));
                    timetableBean.setNote(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_NOTE)));
                    timetableBean.setAlarmState(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_ALARM_STATE)));
                    timetableBean.setMinute(cursor.getInt(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_MINUTE)));
                    timetableBean.setDate(cursor.getString(cursor.getColumnIndex(DbConstant.TIMETABLE.COLUMN_NAME_DATE)));
                }
                cursor.close();
                return timetableBean;
            }
        }
        return timetableBean;
    }


    /**
     * 插入数据
     *
     * @param entity
     **/
    public void saveTimetable(TimetableBean entity) {
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", entity.getName());
            values.put("address", entity.getAddress());
            values.put("note", entity.getNote());
            values.put("date", entity.getDate());
            values.put("time", entity.getTime().getTime());
            values.put("alarm_state", entity.getAlarmState());
            values.put("minute", entity.getMinute());
            if (db.isOpen()) {
                db.insert(DbConstant.TIMETABLE.TABLE_NAME, null, values);//返回新添记录的行
            }
        }
    }

    /**
     * 修复课程表
     *
     * @param entity
     **/
    public void updateTimetable(TimetableBean entity) {
        LogUtil.e("id:" + entity.getId());
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            if (db.isOpen()) {
                ContentValues values = new ContentValues();
                values.put("name", entity.getName());
                values.put("address", entity.getAddress());
                values.put("note", entity.getNote());
                values.put("alarm_state", entity.getAlarmState());
                values.put("minute", entity.getMinute());
                values.put("time", entity.getTime().getTime());
                values.put("date", entity.getDate());
                db.update("tb_timetable", values, "id=?",
                        new String[]{String.valueOf(entity.getId())});
            }
        }

    }

    /**
     * 修改课程表告警状态
     *
     * @param id
     * @param alarmState
     **/
    public void updateAlarmState(Integer id, Integer alarmState) {
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            if (db.isOpen()) {
                db.execSQL("update tb_timetable set alarm_state=? where id=?", new Object[]{alarmState, id});
            }
        }
    }


    /**
     * 删除当前的课程表记录
     *
     * @param id:课程表主键ID
     **/
    synchronized public int deleteTimetable(Integer id) {
        synchronized (dbHelper) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            if (db.isOpen()) {
                int result = db.delete(DbConstant.TIMETABLE.TABLE_NAME, DbConstant.TIMETABLE.COLUMN_NAME_ID + " = ?",
                        new String[]{String.valueOf(id)});
                return result;
            }
        }
        return 0;
    }


}
